package com.songcha.module_bookdetail.ui.dialog

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.songcha.library_base.handler.ApiObservableHandler
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.ui.view.CustomButton
import com.songcha.library_common.ui.view.CustomEditText
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_network.api.ApiCode
import com.songcha.library_network.bean.BaseBean
import com.songcha.module_bookdetail.R
import com.songcha.module_bookdetail.ui.adapter.DialogReportTypeAdapter
import com.songcha.module_bookdetail.ui.decoration.CommentReportTypeItemDecoration
import com.songcha.module_task.api.BookDetailApiManager
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

class CommentReportDialog(context: Context,val mBookId:String,val mCommentId:Int):CustomDialog(context) {

    private lateinit var mView:View
    private var mAdapter:DialogReportTypeAdapter?=null

    private var mTypeList= mutableListOf( "垃圾广告","涉黄低俗","攻击谩骂","违法有害","不实信息","恶意灌水","涉及政治言论","其他类型")

    init {
        setWidth(ScreenUtil.getScreenWidth())
        setDialogTitle("")
        setDialogMsg("")
        setGravity(Gravity.BOTTOM)
        setAnim(com.songcha.library_common.R.style.dialogBottomSlideWindowAnim)
        setShowBtn(false)
        setRadius(floatArrayOf(5f.dp2px(),5f.dp2px(),0f,0f))

    }

    override fun hasView(): Boolean {
        return true
    }

    override fun getView(root:ViewGroup): View? {
        mView= LayoutInflater.from(context).inflate(R.layout.bookdetail_dialog_comment_report,null)
        val lp= FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT,430.dp2px())
        /*lp.leftToLeft= ConstraintLayout.LayoutParams.PARENT_ID
        lp.rightToRight= ConstraintLayout.LayoutParams.PARENT_ID
        lp.bottomToBottom= ConstraintLayout.LayoutParams.PARENT_ID*/
        mView.layoutParams=lp

        return mView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initRcv()
        mView.findViewById<ImageView>(R.id.bookdetail_iv_close).setOnClickListener {
            dismiss()
        }
        mView.findViewById<CustomButton>(R.id.bookdetail_btn_report_comment).setOnClickListener {
            reportComment()
        }
    }

    private fun reportComment(){
        if(mAdapter==null) return
        val idx=mAdapter!!.getSelectIndex()
        if(idx==-1){
            ToastUtil.show("请选择举报类型")
            return
        }
        val content=mView.findViewById<CustomEditText>(R.id.bookdetail_edt_report_comment).text.toString().trim()
        if(content.isBlank()){
            ToastUtil.show("请输入举报理由")
            return
        }
        if(content.length>200){
            ToastUtil.show("举报内容不能超过200个字符")
            return
        }
        if(UserInfoManager.checkJumpLogin()){
            apiReportComment(mTypeList[idx],content)
        }
    }

    private fun apiReportComment(type:String,content:String){
        val json= JSONObject().apply {
            put("bookId",mBookId)
            put("bookCommentId",mCommentId)
            put("typeTitle",type)
            put("content",content)
        }

        ApiObservableHandler.handle(BookDetailApiManager.api.reportComment(json.toString().toRequestBody()),object :IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {
                ToastUtil.show("感谢您的举报")
                dismiss()
            }

        })
    }

    private fun initRcv(){

        val rcv=mView.findViewById<RecyclerView>(R.id.bookdetail_rcv_report_type)
        rcv.apply {
            layoutManager=GridLayoutManager(context,2)
            mAdapter= DialogReportTypeAdapter(context,mTypeList)
            if(itemDecorationCount==0)
                addItemDecoration(CommentReportTypeItemDecoration())
            adapter=mAdapter
        }

    }

}